package action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.lang.Math;

import parser.AddParser;

import storage.DataManager;
import data.Event;
import data.EventDateTime;



public class CheckIf extends Action {

	@Override
	//this function returns an array of events that clash with the user specified time for one day
	public Event[] execute(String s) {
		if (!validateInput(s)) {
			return null;
		}
		String params = s.replace("checkif ", "");
		Event checkIfEvent = AddParser.parseEvent(params);
		if (checkIfEvent != null) {
			return checkIf(checkIfEvent);
		} else {
			return null;
		}
	}

	public Event[] checkIf(Event checkIfEvent) {
		ArrayList<Event> clashingEvents = new ArrayList<Event>();
		long eventStart, eventEnd, checkIfStart, checkIfEnd = Long.MAX_VALUE;
		checkIfStart = checkIfEvent.getStart().getTimeInMilli();

		if (checkIfEvent.getEnd() != null) {
			checkIfEnd = checkIfEvent.getEnd().getTimeInMilli();	
		}

		Event dateOfEvent = new Event();
		Event prevDate = new Event();
		EventDateTime checkIfDate = checkIfEvent.getStart().getDate();
		dateOfEvent.setStart(checkIfDate);
		
		prevDate.setStart(checkIfDate);
	
		Event[] eventsOnSameDay = new Find().find(dateOfEvent);
		checkIfDate.add(Calendar.DATE, -1);
		
		Event[] eventsOnPrevDay  = new Find().find(prevDate);
		
		if (eventsOnSameDay == null && eventsOnPrevDay == null) {
			return new Event[0];
		}

		if(eventsOnSameDay != null) {
		for (Event e : eventsOnSameDay) {
			if (e.getStart().getTime().isDefaultTime()) {
				continue;
			}

			if (e.getEnd() == null || e.getEnd().isDefaultTime()) {
				if(e.getStart().equals(checkIfEvent.getStart())) {
					clashingEvents.add(e);
				}
				continue;
			}

			eventStart = e.getStart().getTimeInMilli();
			eventEnd = e.getEnd().getTimeInMilli();

			if (checkIfStart >= eventStart && checkIfStart <= eventEnd) {
				clashingEvents.add(e);
				continue;
			} else if (checkIfEnd >= eventStart && checkIfEnd <= eventEnd) {
				clashingEvents.add(e);
			}
		}
		}
		
		if(eventsOnPrevDay != null) {
		for (Event e : eventsOnPrevDay) {
			if (e.getStart().getTime().isDefaultTime()) {
				continue;
			}

			if (e.getEnd() == null || e.getEnd().isDefaultTime()) {
				if(e.getStart().equals(checkIfEvent.getStart())) {
					clashingEvents.add(e);
				}
	
				continue;
			}

			eventStart = e.getStart().getTimeInMilli();
			eventEnd = e.getEnd().getTimeInMilli();

			if (checkIfStart >= eventStart && checkIfStart <= eventEnd) {
				clashingEvents.add(e);
				continue;
			} else if (checkIfEnd >= eventStart && checkIfEnd <= eventEnd) {
				clashingEvents.add(e);
			}
		}
		}
		
		Event[] clashingEventsArray = clashingEvents.toArray(new Event[clashingEvents.size()]);

		if(clashingEventsArray == null)
			System.out.println("Null null");
		if(clashingEventsArray.length == 0)
			System.out.println("Nothing found");
		if(clashingEventsArray.length != 0) {
		for(int i =0; i<clashingEventsArray.length;i++) {
			System.out.println(clashingEventsArray[i].toString());
		}
		}
		return clashingEventsArray;
	}

	public Event[] getAllEvents() {

		return DataManager.getAllEventsInStorage();

	}

	private boolean validateInput(String s) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public Event[] undo() {
		return null;
	}

	@Override
	public boolean isUndoable() {
		return false;
	}

	@Override
	public String errorMessage() {
		return "This check could not be performed";
	}

	@Override
	public String getCommandName() {
		return "checkIf";
	}

}
